<?php
class Chinabank {
	//private $_sMid = '21977759';
	//private $_sKey = '51zhuchao68716030';
	private $_sMid;
	private $_sKey;
	
	public function __construct() {
		$this->setMid ();
		$this->setKey ();
	}
	
	public function setMid($sMid = null) {
		if ($sMid) {
			$this->_sMid = $sMid;
		} else {
			$this->_sMid = Base::getConf ( 'chinabank.mid' );
		}
	}
	public function setKey($sKey = null) {
		if ($sKey) {
			$this->_sKey = $sKey;
		} else {
			$this->_sKey = Base::getConf ( 'chinabank.key' );
		}
	}
	
	public function getHtml($sOid, $sMoney, $sReturn = null) {
		if ($sReturn == null) {
			$sReturn = Base::getConf ( 'chinabank.return_url' );
		}
		$sHtml = '<html><header><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></header>';
		$sHtml .= '<body onload="javascript:document.E_FORM.submit()">';
		$sHtml .= '<form method="post" name="E_FORM" action="https://Pay3.chinabank.com.cn/PayGate">' . $this->getForm ( $sOid, $sMoney, $sReturn );
		$sHtml .= '</form></body></html>';
		return $sHtml;
	}
	
	public function getForm($sOid, $sMoney, $sReturn = null, $MoneyType = 'CNY') {
		if ($sReturn == null) {
			$sReturn = Base::getConf ( 'chinabank.return_url' );
		}
		$sForm = '<div id="pay_chinabank">' . "\n";
		$sForm .= '<input type="hidden" name="v_mid" value="' . $this->_sMid . '">' . "\n";
		$sForm .= '<input type="hidden" name="v_oid" value="' . $sOid . '">' . "\n";
		$sForm .= '<input type="hidden" name="v_amount" value="' . $sMoney . '">';
		$sForm .= '<input type="hidden" name="v_moneytype" value="' . $MoneyType . '">' . "\n";
		$sForm .= '<input type="hidden" name="v_url" value="' . $sReturn . '">' . "\n";
		$sMd5 = strtoupper ( md5 ( $sMoney . $MoneyType . $sOid . $this->_sMid . $sReturn . $this->_sKey ) );
		$sForm .= '<input type="hidden" name="v_md5info" value="' . $sMd5 . '">' . "\n";
		$sForm .= '</div>' . "\n";
		return $sForm;
	}
	
	public function checkReceice() {
		$v_oid = trim ( $_POST ['v_oid'] );
		$v_pmode = trim ( $_POST ['v_pmode'] );
		$v_pstatus = trim ( $_POST ['v_pstatus'] );
		$v_pstring = trim ( $_POST ['v_pstring'] );
		$v_amount = trim ( $_POST ['v_amount'] );
		$v_moneytype = trim ( $_POST ['v_moneytype'] );
		$v_md5str = trim ( $_POST ['v_md5str'] );
		$md5string = strtoupper ( md5 ( $v_oid . $v_pstatus . $v_amount . $v_moneytype . $this->_sKey ) );
		if ($v_md5str == $md5string) {
			if ($v_pstatus == "20") {
				return true;
			}
		}
		return false;
	}
	
	public function autoCheckReceice() {
		$v_oid = trim ( $_POST ['v_oid'] );
		$v_pmode = trim ( $_POST ['v_pmode'] );
		$v_pstatus = trim ( $_POST ['v_pstatus'] );
		$v_pstring = trim ( $_POST ['v_pstring'] );
		$v_amount = trim ( $_POST ['v_amount'] );
		$v_moneytype = trim ( $_POST ['v_moneytype'] );
		$remark1 = trim ( $_POST ['remark1'] );
		$remark2 = trim ( $_POST ['remark2'] );
		$v_md5str = trim ( $_POST ['v_md5str'] );
		$md5string = strtoupper ( md5 ( $v_oid . $v_pstatus . $v_amount . $v_moneytype . $this->_sKey ) );
		if ($v_md5str == $md5string) {
			if ($v_pstatus == "20") {
				return true;
			}
		}
		return false;
	}
}
